package com.amumu.drama.admin.modules.order.controller;

import cn.hutool.core.util.StrUtil;
import com.amumu.drama.common.base.api.CommonResult;
import com.amumu.drama.common.modules.order.cache.OrderSettingCache;
import com.amumu.drama.common.modules.order.dto.req.OrderConfigReq;
import com.amumu.drama.common.modules.order.service.OmsOrderSettingService;
import com.amumu.drama.common.modules.order.service.OrderConfigService;
import com.amumu.drama.model.OmsOrderSetting;
import com.amumu.drama.model.order.OrderConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单设置管理Controller
 * Created by macro on 2018/10/16.
 */
@Controller
@Api(tags = "订单设置管理")
@RestController
@RequestMapping("/orderSetting")
public class OmsOrderSettingController {
    @Autowired
    private OmsOrderSettingService orderSettingService;

    @Autowired
    private OrderConfigService orderConfigService;

    @ApiOperation("获取指定订单设置-test")
    @RequestMapping(value = "/getSetting1", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<OmsOrderSetting> getSetting() {
        OmsOrderSetting orderSetting = orderSettingService.getOrderSetting();
        return CommonResult.success(orderSetting);
    }

    @ApiOperation("获取指定订单设置-旧版")
    @RequestMapping(value = "/getSetting", method = RequestMethod.GET)
    public CommonResult<OmsOrderSetting> getItem() {
        OmsOrderSetting orderSetting = orderSettingService.getItem();
        return CommonResult.success(orderSetting);
    }

    @ApiOperation("修改指定订单设置-旧版")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@PathVariable Long id, @RequestBody OmsOrderSetting orderSetting) {
        if (StrUtil.isNotBlank(orderSetting.getManualOrderTime())) {
            String[] split = orderSetting.getManualOrderTime().split("~");
            if (split.length != 2) {
                return CommonResult.failed("手动订单时间设置不正确");
            }
        }
        boolean result = orderSettingService.update(id, orderSetting);
        OrderSettingCache.INSTANCE.initCache();
        return CommonResult.success(result);
    }

    @ApiOperation("获取指定订单设置-新版")
    @GetMapping(value = "/listOrderConfig")
    public CommonResult<List<OrderConfig>> listOrderConfig() {
        return CommonResult.success(orderConfigService.list());
    }

    @ApiOperation("更新订单配置-新版")
    @PostMapping(value = "/updateOrderConfig")
    public CommonResult<Boolean> updateOrderConfig(@RequestBody @Validated OrderConfigReq request) {
        return CommonResult.success(orderSettingService.updateConfigList(request));
    }

}
